package com.example.maxboxtwo.components;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.icu.util.TimeZone;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.maxboxtwo.R;

public class fadsf extends View {
        int y = -6;
        private Paint mPaint;
        private float mRadius;//圆形的半径
        private Path mPath;
        private Bitmap mBitmap;
        private int mAngle=0;//圆形角度
        private final int []xias =new int [10];
        public fadsf(Context context) {
            this(context,null);
        }

        public fadsf(Context context, @Nullable AttributeSet attrs) {
            this(context, attrs,0);
        }

        public fadsf(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }

        private void init() {
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPath = new Path();
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            float a = w/2;
            float b = h/2;
            mRadius = (float) Math.sqrt(a*a+b*b);//根据勾股定理算出圆形的半径
            mBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.led_touch_dot), (int)(w*0.1), (int)(h*0.1), false);
            super.onSizeChanged(w, h, oldw, oldh);

        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.translate(getWidth()/2,getHeight()/2);//将（0，0）点移动到画布中心
            if(mAngle>=360){
                canvas.drawBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.led_ico_heart), getWidth(), getHeight(), false),-getWidth()/2,-getHeight()/2,mPaint);
                return;
            }
//            mPath.reset();//清空路径
//            mPath.moveTo(0,0);
//            mPath.arcTo(new RectF(-mRadius,-mRadius,mRadius,mRadius),45,mAngle,false);//添加闭合的弧形
//            canvas.clipPath(mPath);//裁剪画布为路径的形状
                canvas.drawBitmap(mBitmap,100/2,100/2,mPaint);
//            mAngle++;
//            postInvalidateDelayed(1000);
        }

    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP){
            y+=1;
            postInvalidateDelayed(1000);//重新执行onDraw
        }

        return true;
    }
    }
